//
//  WPCPreviewPresenter.m
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/6.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

#import "WPCPreviewPresenter.h"
#import "WPCPreviewViewController.h"
#import "WPCPreviewView.h"
#import "WPCDBManager.h"

@interface WPCPreviewPresenter()<WPCPreviewViewDelegate>
@property(nonatomic, weak)  WPCPreviewViewController *controller;
@property(nonatomic, strong) WPCThemesItem           *item;
@end

@implementation WPCPreviewPresenter
- (instancetype)initWithThemesItem:(WPCThemesItem*)item
{
    self = [super init];
    if (self) {
        _item = item;
        BOOL ret = [WPCDBManager queryFavoriteWithTitle:item.title];
        item.isFavorite = [NSNumber numberWithBool:ret];
    }
    
    return self;
}

- (void)attachController:(UIViewController*)controller
{
    self.controller = (WPCPreviewViewController*)controller;
}

#pragma mark <WPCPreviewViewDelegate>
- (void)doShare:(CALLBACK)callback
{
    
}

- (void)doDownload:(CALLBACK)callback
{
    
}

- (void)doFavorite:(CALLBACK)callback
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        BOOL ret = NO;
        if (_item.isFavorite.boolValue) {
            ret = [WPCDBManager deleteFavoriteAtItem:_item];
        }
        else {
            ret = [WPCDBManager addFavoriteAtItem:_item];
        }
        
        if (ret) {
            _item.isFavorite = [NSNumber numberWithBool:(!_item.isFavorite.boolValue)];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            callback(_item);
        });
    });
}

@end
